package Tree;

import java.util.Arrays;

/**
 * File Name: TreeNode
 * Date: 2020-07-22 16:23
 * Author: liangling
 * Description 树结构，节点
 */

public class TreeNode {
    int no;
    String name;
    TreeNode left;
    TreeNode right;

    public TreeNode(int no, String name) {
        this.no = no;
        this.name = name;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public TreeNode getLeft() {
        return left;
    }

    public void setLeft(TreeNode left) {
        this.left = left;
    }

    public TreeNode getRight() {
        return right;
    }

    public void setRight(TreeNode right) {
        this.right = right;
    }

    @Override
    public String toString() {
        return "TreeNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }

    public void preOrder(){
        System.out.println(this);//输出父节点
        //递归左子树
        if(null !=this.left){
            this.left.preOrder();
        }

        //递归向右
        if(null != this.right){
            this.right.preOrder();
        }

    }

    //中序遍历
    public void midOrder(){
        //递归左子树
        if(null != this.left){
           this.left.midOrder();
        }

        //输出父节点
        System.out.println(this);

        //递归右子节点
        if(null != this.right){
            this.right.midOrder();
        }
    }

    //后序遍历
    public void lastOrder(){
        //递归左子树
        if (null !=this.left){
            this.left.lastOrder();
        }
        //递归右子树
        if(null != this.right){
            this.right.lastOrder();
        }
        //输出节点
        System.out.println(this);
    }

    //前序查找

    /**
     *
     * @param no 需要查找的数
     * @return 查到就返回
     */
    public TreeNode preSearch(int no){
        //比较当前节点是不是
        if(this.no == no){
            return this;
        }
        //左递归，找到就的那会
        TreeNode resNode = null;
        if (this.left != null){
           resNode= this.left.preSearch(no);
        }
        if (resNode != null){//左子树找到了
            return resNode;
        }
        //左子树没有，找右子树
        if (this.right != null){
            resNode = this.right.preSearch(no);

        }
        return resNode;
    }

    /**
     * 中序遍历查找
     * @param no
     * @return
     */
    public TreeNode midSerch(int no){
        TreeNode resNode = null;
        //左递归
        if (this.left!=null){
           resNode = this.left.midSerch(no);
        }
        if (resNode != null){
            return  resNode;
        }

        if (this.no == no) {
            return this;
        }

        //右递归查找
        if(this.right!=null){
            resNode =  this.right.midSerch(no);
        }

        return resNode;

    }

    /**
     * 后序遍历查找
     * @param no
     * @return
     */
    public TreeNode lastNode(int no){

        TreeNode resNode = null;
        if (this.left != null){
            resNode = this.left.lastNode(no);
        }

        if (resNode != null){
            return resNode;
        }
        //左子树没找到就右子树递归查找
        if (this.right != null){
            resNode = this.right.lastNode(no);
        }
        if(resNode != null){
            return resNode;
        }
        //右子树没找到，判断根节点
        if (this.no == no){
           return this;
        }
        return resNode;
    }

    public void delNode(int no){

        //当前节点的左子节点不为空，并且左子节点就是要删除的节点，将this.left = null;
        if ( null !=this.left  &&this.left.no == no ){
            this.left=null;
            return;
        }
        //当前节点的右子节点不为空，并且右子节点就是要删除的节点，将this.right = null;
        if (null !=this.right  && this.right.no == no){
            this.right = null;
            return;
        }
        //向左子树进行递归删除
        if (null !=this.left){
            this.left.delNode(no);
        }
        //向右子树进行递归删除
        if (null != this.right ){
            this.right.delNode(no);
        }
    }
}

